package com.hlabexamples.fabrevealmenu;

import com.gc.materialdesign.views.ButtonFloat;
import com.hlab.fabrevealmenu.helper.Direction;
import com.hlab.fabrevealmenu.helper.OnFABMenuSelectedListener;
import com.hlab.fabrevealmenu.model.FABMenuItem;
import com.hlab.fabrevealmenu.view.FABRevealMenu;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;

import java.util.ArrayList;

/**
 * BaseFraction
 *
 * @since 2021-05-14
 */
public class BaseFraction extends Fraction implements OnFABMenuSelectedListener {
    private Direction currentDirection = Direction.LEFT;
    private FABRevealMenu fabMenu;
    private DirectionalLayout dlSelect;
    private Text textLeft;
    private Text textUp;
    private Text textSelect;
    private ButtonFloat buttonFloat;
    private Text msgText;
    private Image imgArrow;
    private boolean isXml = true;
    private String codeText = "";
    private ArrayList<FABMenuItem> fabMenuItems;
    /**
     * onBackPressed
     *
     * @return boolean
     */
    public boolean onBackPressed() {
        if (fabMenu != null) {
            if (fabMenu.isShowing()) {
                fabMenu.closeMenu();
                return false;
            }
        }
        return true;
    }

    public FABRevealMenu getFabMenu() {
        return fabMenu;
    }

    public void setFabMenu(FABRevealMenu fabMenu) {
        this.fabMenu = fabMenu;
    }

    /**
     * initListeners
     */
    public void initListeners() {
        textSelect = (Text)getFractionAbility().findComponentById(ResourceTable.Id_t_select);
        dlSelect = (DirectionalLayout)getFractionAbility().findComponentById(ResourceTable.Id_dl_select);
        textLeft = (Text)getFractionAbility().findComponentById(ResourceTable.Id_t_left);
        textUp = (Text)getFractionAbility().findComponentById(ResourceTable.Id_t_up);
        textSelect.setClickedListener(component -> showSelectView(true,null));
        textLeft.setClickedListener(component -> showSelectView(false,Direction.LEFT));
        textUp.setClickedListener(component -> showSelectView(false,Direction.UP));
        msgText = (Text)getFractionAbility().findComponentById(ResourceTable.Id_msg_toast);
        imgArrow = (Image)getFractionAbility().findComponentById(ResourceTable.Id_img_arrow);
        imgArrow.setClickedListener(component -> showSelectView(true,null));
    }

    /**
     * showSelectView
     *
     * @param isShow isShow
     * @param type type
     */
    public void showSelectView(boolean isShow,Direction type) {
        if (isShow) {
            dlSelect.setVisibility(Component.VISIBLE);
        } else {
            dlSelect.setVisibility(Component.HIDE);
        }
        if (!isShow) {
            if (type == Direction.LEFT) {
                textSelect.setText(textLeft.getText());
                if (currentDirection != Direction.LEFT) {
                    currentDirection = Direction.LEFT;
                    fabMenu.setMenuDirection(Direction.LEFT);
                }
            } else if (type == Direction.UP) {
                textSelect.setText(textUp.getText());
                if (currentDirection != Direction.UP) {
                    currentDirection = Direction.UP;
                    fabMenu.setMenuDirection(Direction.UP);
                }
            }
        }
    }

    /**
     * initFabMenu
     */
    public void initFabMenu() {
        buttonFloat = (ButtonFloat)
                getFractionAbility().findComponentById(ResourceTable.Id_fab);
        FABMenuItem fabMenuItem = new FABMenuItem(Constants.ATTACGMENTS, ResourceTable.Graphic_ic_attachment);
        fabMenuItem.setId(Constants.ATTACGMENTS_TYPE);
        FABMenuItem fabMenuItem1 = new FABMenuItem(Constants.IMAGES, ResourceTable.Graphic_ic_image);
        fabMenuItem1.setId(Constants.IMAGES_TYPE);
        FABMenuItem fabMenuItem2 = new FABMenuItem(Constants.PLACES, ResourceTable.Graphic_ic_place);
        fabMenuItem2.setId(Constants.PLACES_TYPE);
        FABMenuItem fabMenuItem3 = new FABMenuItem(Constants.EMOTION, ResourceTable.Graphic_ic_emoticon);
        fabMenuItem3.setId(Constants.EMOTION_TYPE);
        fabMenuItems = new ArrayList<>();
        fabMenuItems.add(fabMenuItem);
        fabMenuItems.add(fabMenuItem1);
        fabMenuItems.add(fabMenuItem2);
        fabMenuItems.add(fabMenuItem3);
        fabMenu = (FABRevealMenu)getFractionAbility().findComponentById(ResourceTable.Id_fabMenu);
        fabMenu.setMenuItems(fabMenuItems);

        if (buttonFloat != null) {
            fabMenu.bindAnchorView(buttonFloat);
            fabMenu.setOnFABMenuSelectedListener(this);
        }
    }

    @Override
    public void onMenuItemSelected(Component view, int id) {
        if (isXml) {
            if (id == Constants.ATTACGMENTS_TYPE) {
                msgText.setText("Attachment Selected");
            } else if (id == Constants.IMAGES_TYPE) {
                msgText.setText("Image Selected");
            } else if (id == Constants.PLACES_TYPE) {
                msgText.setText("Place Selected");
            } else if (id == Constants.EMOTION_TYPE) {
                msgText.setText("Emoticon Selected");
            }
        } else {
            msgText.setText(codeText);
        }
        msgText.setVisibility(Component.VISIBLE);
        getFractionAbility().getMainTaskDispatcher().delayDispatch(() ->
                msgText.setVisibility(Component.HIDE), Constants.INT_3000);
    }

    public void setXml(boolean isXmlView) {
        isXml = isXmlView;
    }

    public void setCodeText(String codeText) {
        this.codeText = codeText;
    }

    public ArrayList<FABMenuItem> getFabMenuItems() {
        return fabMenuItems;
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        currentDirection = Direction.LEFT;
    }
}
